package thread;

/**
 * 互斥锁
 * 当使用多个synchronized锁定多个不同的代码片段，并且指定的锁对象是同一个时，那么这些代码片段
 * 之间就是互斥的
 */
public class SyncDemo6 {
    public static void main(String[] args) {
        Foo foo = new Foo();
        Thread t1 = new Thread(foo::methodA);
        Thread t2 = new Thread(foo::methodB);

        t1.start();
        t2.start();
    }
}
class Foo{
    public synchronized void methodA(){
        Thread t = Thread.currentThread();
        System.out.println(t.getName()+"正在执行A方法...");
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(t.getName()+"执行A方法完毕");
    }

    public synchronized void methodB(){
        Thread t = Thread.currentThread();
        System.out.println(t.getName()+"正在执行B方法...");
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(t.getName()+"执行B方法完毕");
    }
}
